

import requests
import logging

from requests import request


logger = logging.getLogger("request.session")
logging.basicConfig(level=logging.INFO, filename="../requests.log", filemode="w", encoding="utf-8")

class ZlSession(requests.Session):
 def __init__(self,base_url=""):
    self.base_url = base_url
    super().__init__()

 def request(
        self,
        method,
        url,
        *args,**kwargs
 ):
    if not url.startswith("http"):
        url = self.base_url+url
        return super().request(method,url,*args,**kwargs)

 def send(self, request: requests.PreparedRequest,*args,**kwargs):
    logger.info(f"发送请求>>>>>>     接口地址={request.method} {request.url}")
    logger.info(f"发送请求>>>>>>     请求头={request.headers}")
    logger.info(f"发送请求>>>>>>     请求体={request.body}")

    response = super().send(request,**kwargs)
    logger.info(f"接收响应       <<<<< 状态码 = {response.status_code}")
    logger.info(f"接收响应       <<<<< 响应头 = {response.headers}")
    logger.info(f"接收响应       <<<<< 响应体 = {response.content}")

    return response


if __name__ == '__main__':
    session = ZlSession('http://47.107.116.139')
    response = session.get('/phpwind')
    print(response)
